# coding=utf-8
# @Time: 2023/2/12 17:52
# @Author: zyg
# email: zhaoyiguang@lecent.cn
import allure
import pytest

from common.report_add_img import add_img_2_report
from config.driver_config import DriverConfig
from logs.log import log


@pytest.fixture()
@allure.epic("浏览器驱动@pytest.fixture")
def driver():
    log.info("打开浏览器")
    global get_driver
    get_driver = DriverConfig().driver_config()
    yield get_driver
    log.info("退出浏览器......")
    get_driver.quit()


@allure.epic("执行测试用例编码")
def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时，将收集到的item的name和nodeid的中文显示在控制台上
    # 在同级或者父级文件夹下 新建 conftest.py
    # 然后添加一个hook函数
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")


@allure.epic("本次执行测试的用例数")
def pytest_collection_finish(session):
    print("本次执行测试的用例数为：" + str(len(session.items)))


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    # 获取钩子方法的调用结果
    out = yield
    # 从钩子方法的调用结果中获取测试报告
    report = out.get_result()
    report.description=str(item.function.__doc__)

    if report.when == "call":
        if report.failed:
            # 失败截图
            add_img_2_report(get_driver, "失败截图", need_sleep=False)

